import 'package:flutter/material.dart';
import '../../../../widget/editProjectMenu.dart';
import '../../../../widget/createProjectMenu.dart';
import 'interface_info_view.dart';
import 'interface_list_view.dart';

import 'package:get/get.dart';

import '../controllers/interface_controller.dart';
import '../../../../services/screenAdapter.dart';

class InterfaceView extends GetView {
  InterfaceView({super.key, required this.projectId});
  final int projectId;
  final InterfaceController controller = Get.put(InterfaceController());

  Widget _leftNav() {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(
              ScreenAdapter.width(16),
              ScreenAdapter.height(20),
              ScreenAdapter.width(16),
              ScreenAdapter.height(10),
            ),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center, //垂直方向上居中对齐
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Text(
                    "接口列表",
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontWeight: FontWeight.bold, //字体加粗
                      fontSize: ScreenAdapter.fontSize(24),
                    ),
                  ),
                ),
                SizedBox(
                  width: ScreenAdapter.width(80),
                  height: ScreenAdapter.height(80),
                  child: IconButton(
                    onPressed: () {
                      //添加分类
                      Get.dialog(CreateProjectMenu());
                    },
                    icon: Icon(Icons.add, size: ScreenAdapter.fontSize(32)),
                  ),
                ),
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(
              ScreenAdapter.width(16),
              0,
              ScreenAdapter.width(16),
              ScreenAdapter.height(10),
            ),
            child: Container(
              height: ScreenAdapter.height(36),
              decoration: BoxDecoration(
                color: const Color.fromARGB(255, 216, 219, 223),
                borderRadius: BorderRadius.circular(4),
              ),
              alignment: Alignment.center, // 确保容器内容居中
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center, // 行内项目垂直居中
                children: [
                  const Padding(
                    padding: EdgeInsets.only(left: 8),
                    child: Icon(Icons.search, size: 16, color: Colors.grey),
                  ),
                  SizedBox(width: ScreenAdapter.width(4)),
                  Expanded(
                    child: TextField(
                      controller: controller.searchController,
                      decoration: InputDecoration(
                        hintText: '搜索',
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                          fontSize: ScreenAdapter.fontSize(14),
                          color: Colors.grey,
                        ),
                        contentPadding: EdgeInsets.only(bottom: 4), // 微调垂直位置
                        isCollapsed: true,
                      ),
                      style: TextStyle(
                        fontSize: ScreenAdapter.fontSize(14),
                        color: Colors.black,
                        height: 1.0,
                      ),
                      cursorHeight: ScreenAdapter.height(14),
                      cursorColor: Colors.grey,
                      textAlignVertical: TextAlignVertical.center,
                    ),
                  ),
                ],
              ),
            ),
          ),

          Divider(height: ScreenAdapter.height(32)),
          Expanded(
            child: ListView(
              children: [
                // 修改第119-135行的代码
                Obx(() {
                  return Column(
                    children: [
                      // 全部接口（不折叠）
                      InkWell(
                        onTap: () {
                          print("点击了全部接口");
                          controller.changeCurrentViewIndex(0, 0, 0);
                        },
                        child: ListTile(title: const Text('全部接口')),
                      ),
                      ...controller.leftProjectMenuList.map((value) {
                        if (value == null) return Container();

                        return Theme(
                          data: ThemeData().copyWith(
                            dividerColor: Colors.transparent, // 去除默认分割线
                          ),
                          child: MouseRegion(
                            onEnter: (_) =>
                                controller.setHoverState(value.id ?? 0, true),
                            onExit: (_) =>
                                controller.setHoverState(value.id ?? 0, false),
                            child: Obx(() {
                              return ExpansionTile(
                                onExpansionChanged: (bool expanded) {
                                  controller.changeCurrentViewIndex(
                                    0,
                                    value.id ?? 0,
                                    0,
                                  );
                                },
                                title: Text(value.name ?? ''),
                                // 根据悬停状态决定显示什么
                                trailing:
                                    controller.hoverStates[value.id] == true
                                    ? Row(
                                        mainAxisSize:
                                            MainAxisSize.min, // 防止占用过多空间
                                        children: [
                                          IconButton(
                                            icon: const Icon(
                                              Icons.edit,
                                              size: 20,
                                            ),
                                            onPressed: () {
                                              Get.dialog(EditProjectMenu(menu_id:value.id??0));
                                            },
                                            tooltip: '编辑',
                                          ),
                                          IconButton(
                                            icon: const Icon(
                                              Icons.delete,
                                              size: 20,
                                            ),
                                            onPressed: () {
                                              Get.defaultDialog(
                                                title: "提示信息!",
                                                middleText: "您确定要删除吗?",
                                                confirm: ElevatedButton(
                                                  onPressed: () {
                                                    controller.deleteProjectMenu(
                                                      value.id ?? 0,
                                                    );
                                                    Get.back();
                                                  },
                                                  child: const Text("确定"),
                                                ),
                                                cancel: ElevatedButton(
                                                  onPressed: () {
                                                    Get.back();
                                                  },
                                                  child: const Text("取消"),
                                                ),
                                              );
                                            },
                                            tooltip: '删除',
                                          ),
                                        ],
                                      )
                                    : null,
                                children: [
                                  ...(value.children?.map((e) {
                                        if (e == null) return Container();
                                        return InkWell(
                                          onTap: () {
                                            controller.changeCurrentViewIndex(
                                              1,
                                              value.id ?? 0,
                                              e.id ?? 0,
                                            );
                                          },
                                          // 选中状态背景
                                          highlightColor: const Color(
                                            0xFF3498DB,
                                          ),
                                          child: Container(
                                            height: ScreenAdapter.height(48),
                                            padding: EdgeInsets.only(
                                              left: ScreenAdapter.width(40),
                                              right: ScreenAdapter.width(16),
                                            ),
                                            alignment: Alignment.centerLeft,
                                            decoration: BoxDecoration(
                                              color:
                                                  controller
                                                          .currentInterfaceId
                                                          .value ==
                                                      e.id
                                                  ? Color(0xFF1890FF)
                                                  : Colors.transparent,
                                            ),
                                            child: Row(
                                              mainAxisAlignment:
                                                  MainAxisAlignment
                                                      .spaceBetween,
                                              children: [
                                                Text(
                                                  e.name ?? '',
                                                  style: TextStyle(
                                                    color:
                                                        controller
                                                                .currentInterfaceId
                                                                .value ==
                                                            e.id
                                                        ? Colors.white
                                                        : Colors.black,
                                                    fontSize:
                                                        ScreenAdapter.fontSize(
                                                          14,
                                                        ),
                                                    fontWeight: FontWeight.w500,
                                                  ),
                                                ),
                                                IconButton(
                                                  icon: Icon(Icons.delete),
                                                  iconSize:
                                                      ScreenAdapter.fontSize(
                                                        14,
                                                      ),
                                                  onPressed: () {
                                                    controller.deleteInterface(
                                                      e.id ?? 0,
                                                    );
                                                  },
                                                ),
                                              ],
                                            ),
                                          ),
                                        );
                                      }).toList() ??
                                      []),
                                ],
                              );
                            }),
                          ),
                        );
                      }).toList(),
                      // 可折叠的分类
                    ],
                  );
                }),
                // 可折叠的分类
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    controller.setProjectId(projectId);
    return Scaffold(
      body: Container(
        margin: EdgeInsetsDirectional.all(ScreenAdapter.width(20)),
        child: Row(
          children: [
            Expanded(flex: 2, child: _leftNav()),
            SizedBox(width: ScreenAdapter.width(10)),
            Expanded(
              flex: 8,
              child: Obx(
                () => controller.currentViewIndex.value == 0
                    ? InterfaceListView()
                    : InterfaceInfoView(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
